package com.szqhyxj.youxiangju.biz.onlineChannel;

import android.view.View;

import com.szqhyxj.youxiangju.BaseConsumer;
import com.szqhyxj.youxiangju.sys.AbBasePresenter;
import com.szqhyxj.youxiangju.sys.BaseResult;

import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

class OnlineChannelPresenter extends AbBasePresenter implements OnlineChannelContract.Presenter {

    private final OnlineChannelContract.View view;

    public OnlineChannelPresenter(OnlineChannelContract.View view) {
        this.view = view;
    }

    @Override
    public void initChannelList(String cardId, String tradeType) {
        ChannelModel.getChannelList(cardId, tradeType)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Consumer<BaseResult<ChannelModel.ChannelEntity>>() {
                    @Override
                    public void accept(BaseResult<ChannelModel.ChannelEntity> channelEntityBaseResult) throws Exception {
                        view.dismissLoading();
                        if (channelEntityBaseResult.businessLogicSuccess()) {
                            List<ChannelModel.RowsItem> rowsItem = channelEntityBaseResult.getData().getRows();
                            if (rowsItem.size() == 0) {
                                view.showNone();
                            } else {
                                view.showChannelList(rowsItem);
                            }

                        } else {
                            view.showToast(channelEntityBaseResult.getMessage() + "");
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        view.dismissLoading();
                        view.showToast(throwable.getMessage() + "");
                    }
                });

    }


    /**
     * 申请绑卡
     *
     * @param cardid
     * @param channelId
     * @param frontUrl
     */
    @Override
    public void getMessage(String cardid, String channelId, String frontUrl, int type) {
        view.showLoading();
        ChannelModel.getbindCard(cardid, channelId, frontUrl)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Consumer<BaseResult<String>>() {
                    @Override
                    public void accept(BaseResult<String> stringBaseResult) throws Exception {
                        if (stringBaseResult.businessLogicSuccess()) {
                            view.dismissLoading();
                            if (stringBaseResult.getMessage().equals("用户已绑卡")) {
                                view.enshowSMSview();
                                view.showToast(stringBaseResult.getMessage());
                            } else {
                                view.showToast(stringBaseResult.getMessage());
                            }

                        } else {
                            view.dismissLoading();
                            view.showToast(stringBaseResult.getMessage());
                            view.enshowSMSview();
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        view.dismissLoading();
                        view.showToast(throwable.getMessage());
                        view.enshowSMSview();
                    }
                });

    }

    @Override
    public void commitSms(String smsCode, String cardId, String channelId) {
        view.showLoading();
        ChannelModel.confirmBindCard(smsCode, cardId, channelId)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new BaseConsumer<String>() {
                    @Override
                    protected void error(String code, String msg) {
                        view.dismissLoading();
                        view.showToast(msg);
                    }

                    @Override
                    protected void successAccept(String s) {
                        view.dismissLoading();
                        view.enshowSMSview();
                        view.showToast("绑卡成功！");

                    }
                });

    }


    @Override
    public void addDisposables(Disposable disposable) {
        super.addDisposables(disposable);
        view.showLoading();
    }


}